<template>
  <div>
    <van-nav-bar title="编辑资料" left-arrow @click-left="$router.go(-1)" />
    <div class="avatar">
      <van-image
        width="120"
        height="120"
        fit="cover"
        round
        :src="userInfo.photo"
      />
      <van-uploader :after-read="afterRead" />
    </div>
    <van-cell-group>
      <van-cell
        title="名称"
        :value="userInfo.name"
        is-link
        @click="showDialogClick"
      />
      <van-cell
        title="性别"
        :value="userInfo.gender ? '女' : '男'"
        is-link
        @click="showPopup = true"
      />
      <van-cell
        title="生日"
        :value="userInfo.birthday"
        is-link
        @click="datePickerClick"
      />
    </van-cell-group>
    <van-dialog
      v-model="showDialog"
      title="编辑名称"
      show-cancel-button
      @confirm="editName"
    >
      <van-field ref="input" v-model="newName" placeholder="请输入名称" />
    </van-dialog>
    <van-popup v-model="showPopup" position="bottom">
      <van-nav-bar
        title="修改性别"
        left-arrow
        @click-left="showPopup = false"
      />
      <van-cell title="男" is-link @click="editGender(0)" />
      <van-cell title="女" is-link @click="editGender(1)" />
    </van-popup>
    <van-popup v-model="showDatePicker" position="bottom">
      <van-datetime-picker
        @confirm="editBirthday"
        @cancel="showDatePicker = false"
        v-model="currentDate"
        type="date"
        title="选择年月日"
        :min-date="minDate"
        :max-date="maxDate"
      />
    </van-popup>
    <div class="cropper-box" v-show="showCropper">
      <vueCropper
        ref="cropper"
        :img="img"
        autoCrop
        autoCropWidth="120"
        autoCropHeight="120"
      ></vueCropper>
      <van-button class="concel" type="primary" @click="showCropper = false"
        >取消</van-button
      >
      <van-button class="confirm" type="info" @click="editCropper"
        >确定</van-button
      >
    </div>
  </div>
</template>

<script>
//导入mapState辅助函数
import { mapState } from 'vuex'
import { editUserInfo, editPhoto } from './user.js'
import * as moment from 'moment'
import { VueCropper } from 'vue-cropper'
export default {
  name: 'edit',
  data () {
    return {
      showDialog: false,
      newName: '',
      showPopup: false,
      showDatePicker: false,
      minDate: new Date(1970, 0, 1),
      maxDate: new Date(),
      currentDate: new Date(),
      img: null,
      showCropper: false
    }
  },
  components: {
    VueCropper
  },
  methods: {
    showDialogClick () {
      this.newName = this.userInfo.name
      this.showDialog = true
      this.$nextTick(() => {
        this.$refs.input.focus()
      })
    },
    editName () {
      editUserInfo({ name: this.newName }).then(res => {
        // console.log(res);
        this.$toast.success('修改姓名成功')
        this.$store.commit('updateUserInfo', {
          ...this.userInfo,
          name: this.newName
        })
      })
    },
    editGender (gender) {
      editUserInfo({ gender }).then(res => {
        console.log(res)
        this.showPopup = false
        this.$toast.success('修改性别成功')
        this.$store.commit('updateUserInfo', {
          ...this.userInfo,
          gender
        })
      })
    },
    editBirthday () {
      let birthday = moment(this.currentDate).format('YYYY-MM-DD')
      // console.log(birthday);
      editUserInfo({ birthday }).then(res => {
        this.showDatePicker = false
        this.$toast.success('修改生日成功')
        this.$store.commit('updateUserInfo', {
          ...this.userInfo,
          birthday
        })
      })
    },
    datePickerClick () {
      this.showDatePicker = true
      let date = new Date(this.userInfo.birthday)
      this.currentDate = date
    },
    afterRead (file) {
      // 此时可以自行将文件上传至服务器
      // console.log(file);
      this.img = file.content
      this.showCropper = true
    },
    editCropper () {
      this.$refs.cropper.getCropBlob(data => {
        // do something
        // console.log(data)
        let fd = new FormData()
        fd.append('photo', data)
        editPhoto(fd).then(res => {
          // console.log(res);
          this.$toast.success('头像修改成功')
          this.showCropper = false

          this.$store.commit('updateUserInfo', {
            ...this.userInfo,
            photo: res.data.data.photo
          })
        })
      })
    }
  },
  computed: {
    //将vuex数据隐射成计算属性
    ...mapState(['userInfo'])
  }
}
</script>

<style lang="less">
.avatar {
  height: 160px;
  display: flex;
  justify-content: center;
  align-items: center;
  .van-uploader {
    position: absolute;
    width: 130px;
    height: 120px;
    opacity: 0;
    .van-uploader__upload {
      width: 130px;
      height: 120px;
    }
  }
}
.cropper-box {
  position: absolute;
  left: 0;
  right: 0;
  top: 0;
  bottom: 0;
  .concel {
    position: absolute;
    left: 0;
    bottom: 0;
  }
  .confirm {
    position: absolute;
    right: 0;
    bottom: 0;
  }
}
</style>
